import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

public class Server implements Runnable {
	private static ServerSocket server;
	
	private static HashMap<Socket, String> socketMap;
	
	private static Connection connection;
	private static Statement statement;

	public static void main(String[] args) {
		socketMap = new HashMap<Socket, String>();
		
		try {
			Class.forName("org.sqlite.JDBC");
			
			connection = DriverManager.getConnection("jdbc:sqlite:test.db");
			connection.setAutoCommit(false);
			
			System.out.println("Open database successfully");
			
			statement = connection.createStatement();
			
			statement.executeUpdate("DROP TABLE user");
			statement.executeUpdate("CREATE TABLE user (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT NOT NULL, userid INTEGER NOT NULL, userpassword INTEGER NOT NULL)");
			statement.close();
			
			connection.commit();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		new Thread(new Server()).start();
	}

	@Override
	public void run() {
		try {
			server = new ServerSocket(1010);
			
			System.out.println("Server is waiting for contacting!");
			
			while(true) {
				int userid = 0;
				String useridString = "";
				
				statement = connection.createStatement();
				
				try {
					ResultSet rSet = statement.executeQuery("SELECT * FROM user");
					
					while(rSet.next()) {
						userid = rSet.getInt("userid");
					}
					
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
				if(userid == 0) {
					for(int i = 0; i < 9; i++) {
						Random random = new Random();
						
						useridString += random.nextInt(10);
						
						userid = new Integer(useridString);
					}
				} else {
					userid += 1;
				}
				
				Socket socket = server.accept();
				
				PrintWriter pw = new PrintWriter(socket.getOutputStream());
				pw.println("Your userid is " + userid);
				pw.flush();
				
				BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				
				String[] clientMsgs = br.readLine().split(":");
				
				String username = clientMsgs[0];
				String userpassword = clientMsgs[1];
				
				System.out.println(username + " is contacted");
				
				socketMap.put(socket, username);
				
				statement = connection.createStatement();
				statement.executeUpdate("INSERT INTO user(username, userid, userpassword) VALUES (\'" + username + "\'," + userid + "," + userpassword + ")");
				
				statement.close();
				connection.commit();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}


























